package org.seedltd.upms.controller.detection;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.seedltd.core.base.BaseController;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.validation.Save;
import org.seedltd.core.validation.Update;
import org.seedltd.upms.constant.GlobalConst;
import org.seedltd.upms.model.detection.form.DetectionTerminalForm;
import org.seedltd.upms.model.detection.form.DetectionTerminalPipelineForm;
import org.seedltd.upms.model.detection.form.DetectionTerminalSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionTerminalPipelineVO;
import org.seedltd.upms.model.detection.vo.DetectionTerminalVO;
import org.seedltd.upms.service.detection.DetectionTerminalPipelineService;
import org.seedltd.upms.service.detection.DetectionTerminalService;
import org.seedltd.upms.service.detection.VendingMachineApiService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.google.zxing.WriterException;

import lombok.AllArgsConstructor;

/**
 *
 * 终端管理控制器
 * 
 * @author laizuan
 * @version 1.0
 * @since 2021/07/28
 */
@Controller
@AllArgsConstructor
@RequestMapping("/detectionTerminal")
public class DetectionTerminalController extends BaseController<DetectionTerminalService> {
    private final DetectionTerminalPipelineService detectionTerminalPipelineService;
    private final VendingMachineApiService vendingMachineApiService;

    /**
     * 新增
     * 
     * @param form
     *            终端管理表单数据
     * @return 终端管理主键
     */
    @ResponseBody
    @PostMapping(value = "/add")
    @PreAuthorize("hasAnyAuthority('detection:rb:add', 'detection:vm:add')")
    public Long add(@RequestBody @Validated({Save.class}) DetectionTerminalForm form)
        throws IOException, WriterException {
        return baseService.add(form);
    }

    /**
     * 获取详情
     * 
     * @param id
     *            终端管理主键
     * @return 终端管理
     */
    @ResponseBody
    @GetMapping(value = "/get")
    public DetectionTerminalVO get(Long id) {
        Assert.notNull(id, "参数不能为空");
        return baseService.findById(id);
    }

    /**
     * 修改
     * 
     * @param form
     *            终端管理表单数据
     * @return 修改状态。true标识修改成功，false标识修改失败
     */
    @PostMapping(value = "/update")
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('detection:rb:update', 'detection:vm:update')")
    public boolean update(@RequestBody @Validated({Update.class, Save.class}) DetectionTerminalForm form) {
        return baseService.update(form);
    }

    /**
     * 删除
     * 
     * @param id
     *            终端管理主键
     * @return 删除状态。true标识修改成功，false标识修改失败
     */
    @ResponseBody
    @GetMapping(value = "/delete")
    @PreAuthorize("hasAnyAuthority('detection:rb:delete','detection:vm:delete')")
    public boolean delete(Long id) {
        Assert.notNull(id, "ID不能为空");
        return baseService.delete(id);
    }

    /**
     * 列表
     * 
     * @param searchForm
     *            查询条件
     * @return 终端管理列表集合
     */
    @ResponseBody
    @PostMapping(value = "/list")
    public BasePage<DetectionTerminalVO> page(@RequestBody DetectionTerminalSearchForm searchForm) {
        return baseService.page(searchForm);
    }

    /**
     * 更新货道信息
     * 
     * @param id
     *            售卖机主键
     * @return
     */
    @ResponseBody
    @GetMapping("/pipeline/{id}")
    @PreAuthorize("hasAnyAuthority('detection:vm:update')")
    public boolean updatePipeline(@PathVariable Long id) {
        return baseService.updatePipeline(id);
    }

    /**
     * 删除货道信息
     *
     * @param id
     *            货道主键
     * @return
     */
    @ResponseBody
    @GetMapping("/deletePipeline/{id}")
    @PreAuthorize("hasAnyAuthority('detection:vm:update')")
    public boolean deletePipeline(@PathVariable Long id) {
        return detectionTerminalPipelineService.removeById(id);
    }

    /**
     * 刷新售货机 api token
     */
    @ResponseBody
    @GetMapping("/refreshThirdToken")
    @PreAuthorize("hasAnyAuthority('detection:vm:token')")
    public boolean refreshThirdToken() {
        vendingMachineApiService.login();
        return true;
    }

    /**
     * 获取货道列表
     * 
     * @param id
     *            售卖机主键
     * @return
     */
    @ResponseBody
    @GetMapping("/pipelineList/{id}")
    @PreAuthorize("hasAnyAuthority('detection:vm:update')")
    public List<DetectionTerminalPipelineVO> listPipeline(@PathVariable Long id) {
        return detectionTerminalPipelineService.listByTerminalId(id);
    }

    /**
     * 更新货道信息
     * 
     * @param forms
     *            货道信息
     * @return
     */
    @ResponseBody
    @PostMapping("/savePipeline")
    @PreAuthorize("hasAnyAuthority('detection:vm:update')")
    public boolean savePipeline(@RequestBody @Validated(Save.class) List<DetectionTerminalPipelineForm> forms) {
        Assert.notEmpty(forms, "forms not be null");
        return detectionTerminalPipelineService.update(forms);
    }

    /**
     * 刷新二维码编码
     * 
     * @param id
     *            主键
     * @return
     */
    @ResponseBody
    @GetMapping(value = "/refreshQrCode")
    @PreAuthorize("hasAnyAuthority('detection:rb:update', 'detection:vm:update')")
    public boolean refreshQrCode(Long id) throws IOException, WriterException {
        Assert.notNull(id, "ID不能为空");
        return baseService.refreshQrCode(id);
    }

    /**
     * 下载二维码编码
     * 
     * @param id
     *            主键
     * @param isPreview
     *            是否预览
     * @return
     */
    @GetMapping(value = "/download/{id}/{isPreview}")
    public void download(HttpServletResponse response, @PathVariable Long id, @PathVariable Integer isPreview)
        throws IOException {
        Assert.notNull(id, "ID不能为空");
        baseService.downloadQrCode(response, id, GlobalConst.YES.equals(isPreview));
    }
}